<template>
  <div>
    <PageWrapper :contentFullHeight="true" :contentBackground="true">
      <BasicTable @register="registerTable" :searchInfo="searchInfo">
        <template #toolbar>
          <a-button v-auth="['system:account:create']" type="primary" @click="handleCreate">{{
            t('routes.table.accountManagement.addAccount')
          }}</a-button>
        </template>
        <!-- 创建时间 -->
        <template #createTime="{ text: createTime }">
          <span>
            {{ proxy.$filters.filterTime(createTime) }}
          </span>
        </template>
        <!-- 个人印章 -->
        <template #sign="{ text: sign }">
          <span>
            {{
              sign === ''
                ? t('routes.table.accountManagement.no')
                : t('routes.table.accountManagement.yes')
            }}
          </span>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                icon: 'ion:ios-locked-outline',
                tooltip: t('routes.table.accountManagement.frozen'),
                popConfirm: {
                  title: t('routes.table.accountManagement.isFrozen'),
                  confirm: handleFrozen.bind(null, record),
                },
                ifShow: record.status === 0,
                auth: 'system:account:freeze',
              },
              {
                icon: 'ion:ios-unlocked-outline',
                color: 'error',
                tooltip: t('routes.table.accountManagement.unfrozen'),
                popConfirm: {
                  title: t('routes.table.accountManagement.isUnfrozen'),
                  confirm: handleUnFrozen.bind(null, record),
                },
                ifShow: record.status === -1,
                auth: 'system:account:unfreeze',
              },
              {
                icon: 'ion:eye-outline',
                tooltip: t('routes.table.accountManagement.viewAccountDetail'),
                onClick: handleView.bind(null, record),
                auth: 'system:account:detail',
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: t('routes.table.accountManagement.editUserDetail'),
                onClick: handleEdit.bind(null, record),
                auth: 'system:account:update',
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: t('routes.table.accountManagement.deleteAccount'),
                popConfirm: {
                  title: t('routes.table.accountManagement.confirmDeleteAccount'),
                  confirm: handleDelete.bind(null, record),
                },
                auth: 'system:account:delete',
              },
            ]"
          />
        </template>
      </BasicTable>
      <AccountModal @register="registerModal" @success="handleSuccess" />
      <AccountDetail @register="registerDetailModal" />
    </PageWrapper>
  </div>
</template>
<script lang="ts" setup>
  import { reactive, getCurrentInstance } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    Getallpersondto,
    AddUserData,
    UpdateUserData,
    DeleteUserData,
    BlockedPerson,
  } from '/@/api/demo/system';
  import { PageWrapper } from '/@/components/Page';

  import { useModal } from '/@/components/Modal';
  import AccountModal from './AccountModal.vue';
  import AccountDetail from './AccountDetail.vue';

  import { columns, searchFormSchema } from './account.data';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  const { proxy }: any = getCurrentInstance();
  const { createMessage } = useMessage();

  const [registerModal, { openModal }] = useModal();
  const [registerDetailModal, { openModal: openDetailModal }] = useModal();

  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload }] = useTable({
    // title: t('routes.table.accountManagement.accountList'),
    titleHelpMessage: 'AccountIndex',
    api: Getallpersondto,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    canResize: false,
    handleSearchInfoFn(info) {
      return info;
    },
    actionColumn: {
      width: 150,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function handleView(record: Recordable) {
    openDetailModal(true, { record });
  }

  function handleDelete(record: Recordable) {
    DeleteUserData(record.id).then(() => {
      reload();
      createMessage.success(t('common.delSuccessText'));
    });
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      UpdateUserData(values).then(() => {
        reload();
        createMessage.success(t('common.updateSuccessText'));
      });
    } else {
      AddUserData(values).then(() => {
        reload();
        createMessage.success(t('common.saveSuccess'));
      });
    }
  }

  // 冻结
  const handleFrozen = (record: Recordable) => {
    BlockedPerson(record.id).then(() => {
      reload();
      createMessage.success(t('routes.table.accountManagement.frozenSuccess'));
    });
  };

  // 解冻
  const handleUnFrozen = (record: Recordable) => {
    console.log(record);
  };
</script>
